package com.ossean.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ossean.dao.DBDest;
import com.ossean.dao.DBSource;
import com.ossean.dao.GatherDao;
import com.ossean.dao.TransferPrjDao;
import com.ossean.model.EddRelations;
import com.ossean.model.GatherProjectsModel;
import com.ossean.model.OpenSourceProject;
import com.ossean.model.Synonymmings;
import com.ossean.model.Synonyms;
import com.ossean.model.Taggings;

@Component("transferProjectsUtil")
public class TransferProjectsUtil {
	Logger logger = Logger.getLogger(this.getClass());
	
	@Resource
	private DBSource dbSource;
	@Resource
	private DBDest dbDest;
	@Resource
	private GatherDao gatherDao;
	@Resource
	private TransferPrjDao transferPrjDao;
	
	
	private String gatherProjectTableName = "gather_projects_test";
	private String openSourceProjectTableName = "open_source_projects_17";
	private String tagTableName = "tags_17";
	private String taggingTableName = "taggings_17";
	private String eddRelationTableName = "edd_relations_17";
	private String pointerTableName = "edd_pointers_17";
	private String sourceTableName = "edd_relations_17";
	private String targetTableName = "open_source_projects_17";
	
	private String synonymTableName = "synonyms_17";
	private String synonymmingTableName = "synonymmings_17";
	
	private static int NUMBER_OF_TAGS = 50;
	private static int NUMBER_OF_CATEGORIES = 5;
	private static int NUMBER_OF_LANGUAGES = 5;
	
	
	public static String getTargetTable(int ospId){
		String targetTableName = "";
		if(ospId >= 770000){
			targetTableName = "relative_memo_to_open_source_projects_70";
		}
		else{
			int a = 1 + ospId/11000;
			targetTableName = "relative_memo_to_open_source_projects_" + a;
		}

		return targetTableName;
	}

	
	/**
	 * 处理一个edd_relation
	 * @param eddRelation
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public void handleOneRelation(EddRelations relation){
		logger.info("处理edd_relations表数据，id：" + relation.getId());
		List<GatherProjectsModel> gatherProjectList = new ArrayList<GatherProjectsModel>();//最热门项目要放在第一个下标的位置
		String gather_projects_ids = relation.getGather_projects_ids();
		gather_projects_ids = gather_projects_ids.substring(1, gather_projects_ids.length() - 1);
		String[] idsArray = gather_projects_ids.split(",");
		boolean error_lost = false;
		for(int i = 0; i < idsArray.length; i++){
			String id = idsArray[i];
			GatherProjectsModel p = gatherDao.selectGPMById(gatherProjectTableName, Integer.parseInt(id));
			if (p != null)//防止因为数据不一致导致的没有取到相应的数据
				gatherProjectList.add(p);
			else{
				//删除没有的id
				error_lost = true;
				idsArray[i] = null;
			}
		}
		if(error_lost){
			//需要更新gather_projects_ids字段
			String gather_projects_ids_new = ",";
			for(String id:idsArray)
				if(id != null)
					gather_projects_ids_new = gather_projects_ids_new + id + ",";
			if(gather_projects_ids_new.length() > 1){
				//更新gather_projects_ids字段
				transferPrjDao.updateEddRelationGatherProjectsIds(eddRelationTableName, relation.getId(), gather_projects_ids_new);
			}else{
				//删除该relation记录
				dbSource.deleteEddRelationsItem(eddRelationTableName, relation.getId());
			}
		}
		OpenSourceProject osp = mergeGatherProjectsToOpenSourceProject(gatherProjectList);
		if(osp != null){
			boolean sameAlready = hasSameOsp(gather_projects_ids, osp);
			if(!sameAlready){
				//将数据插入open_source_projects表中
				dbDest.insertOsp(openSourceProjectTableName, osp);
//				int osp_id = dbDest.getAutoIncrementOspId(openSourceProjectTableName, osp);//系统没有考虑目的数据库事务操作
				int osp_id = osp.getId();
				//tag分离
				String tags = osp.getTags();
				if(tags == null){
	        		//表示该项目没有标签
	        		return;
	        	}
	        	String[] tagArray = tags.split(",");
	        	for(String tag:tagArray){
	        		tag = tag.substring(1, tag.length() - 1).toLowerCase();//去掉两边的尖括号 并转换为小写
	        		dbDest.insertTag(tagTableName, tag);//ignore方式插入该项目的标签
	        		int tag_id = dbDest.selectTagIdByName(tagTableName, tag);
	        		Taggings tagging = new Taggings();
	        		tagging.setTag_id(tag_id);
	        		tagging.setTaggable_id(osp_id);
	        		tagging.setTaggable_type("OpenSourceProject");
	        		tagging.setContext("tags");
	        		tagging.setCreated_at(DateHandler.getNow());
	        		tagging.setDisagree_num(50);
	        		//将Taggings对象存入数据库中
	        		try {
	            		dbDest.insertTagging(taggingTableName, tagging);
	        		} catch (Exception e) {
	        			//在插入同义词时 open_source_projects中的记录已经被删除掉了
	    				logger.info("osp_id has been removed");
	    				return;
	        		}
	        	}
			}
		}

		//更新指针
		int end_id = relation.getId();
		dbSource.updatePointer(pointerTableName, sourceTableName, targetTableName, end_id+1);
	}
	
	//将GatherProject的list合并为OpenSourceProject
	//需要将最热门项目存储到第一个下标的位置
	public OpenSourceProject mergeGatherProjectsToOpenSourceProject(List<GatherProjectsModel> gatherProjectList){
		if(gatherProjectList.size() == 0 || gatherProjectList == null)
			return null;
		OpenSourceProject result = new OpenSourceProject();
		GatherProjectsModel hottestProject = getHottestProject(gatherProjectList);
		if(hottestProject == null){
			//表示没有找到最热门的项目 只有freecode项目
			return null;
		}
		int id = hottestProject.getId();
		String name = hottestProject.getName();
		String description = null;
		String url = null;
		String url_md5 = null;
		String language = null;
		String category = null;
		String source = mergeSource(gatherProjectList, hottestProject.getId());
//		String updated_at = null; 直接在sql语句中用now()插入或更新
		String tags = null;
		String tags_for_search = null;
		String synonyms = "";
		String license = hottestProject.getLicense();
		String homepage = hottestProject.getHomepage();
		String updated_time = hottestProject.getUpdated_time();
		String extracted_time = hottestProject.getExtracted_time().toString();
		String created_time = hottestProject.getCreated_time();
		
		
		/**
		 * 将gatherProjectList列表中的最热门项目放在第一个下标的位置
		 */
		gatherProjectList = putHottestInHead(gatherProjectList, id);
		
		for(GatherProjectsModel gatherProject:gatherProjectList){
			tags = mergeTextWithoutSource(tags, gatherProject.getTags(), NUMBER_OF_TAGS);
			
//			//处理同义词 获取每个项目最本质的同义词作为结果
//			List<String> synonyms_tmp = getSynonyms(gatherProject);
//			for(String s:synonyms_tmp){
//				if(!s.equals(hottestProject.getName().toLowerCase()))
//					synonyms = synonyms + "<" + s + ">,"; //如果与项目名同名则不加入
//			}
			
			List<Integer> synonymList = dbSource.getSynonymmingList(synonymmingTableName,gatherProject.getId());
			for(int i=0;i<synonymList.size();i++){
				int synonym_id = synonymList.get(i);
//				int relate_count = dbSource.selectSynonymmingCount(synonymmingTableName, synonym_id);
//				if(relate_count>=2)
//					continue;
				String synonym_name = dbSource.getSynonymNameById(synonymTableName,synonym_id);
				if(dbDest.selectPrjNumFromOSP(targetTableName,'<'+synonym_name+'>') >=1)
					continue;
				if(!synonym_name.toLowerCase().equals(hottestProject.getName().toLowerCase())){
					synonyms = synonyms + "<" + synonym_name + ">,"; //如果与项目名同名则不加入
				}
			}
			
			
			if(!(("FreeCode".toLowerCase()).equals(gatherProject.getSource().toLowerCase()))){
				//表示如果是FreeCode社区中的项目 只将tags属性合并 不进行下面的步骤
				description = mergeTextWithSource(description, gatherProject.getDescription(), gatherProject.getSource());
//				followers_num = hottestProject.getFollowers_num() == null ? 0 : hottestProject.getFollowers_num();
				url = mergeTextWithSource(url, gatherProject.getUrl(), gatherProject.getSource());
				url_md5 = mergeTextWithSource(url_md5, gatherProject.getUrl_md5(), gatherProject.getSource());
				language = mergeTextWithoutSource(language, gatherProject.getLanguage(),NUMBER_OF_LANGUAGES);
//				download_num = hottestProject.getDownload_num() == null ? 0 : hottestProject.getDownload_num();
				//view_num_crawled暂不处理
				category = mergeTextWithoutSource(category, gatherProject.getCategory(),NUMBER_OF_CATEGORIES);
//				crawled_time = mergeTime(crawled_time, gatherProject.getCrawled_time());
				//source = mergeSource(source, gatherProject.getSource());
//				//created_time = mergeTime(created_time, gatherProject.getRegistered_time());
//				//updated_time = mergeTime(updated_time, gatherProject.getLast_update_time());
			}
		}
		
		if("".equals(synonyms))
			synonyms = null;
		else{
			synonyms = synonyms.substring(0, synonyms.length()-1);
			//对synonyms做一步去重复的操作
			synonyms = mergeTextWithoutSource(synonyms, "", -1); //表示保留所有结果
		}
		result.setId(id); 
		result.setName(name);
		result.setDescription(description);
		result.setUrl(url);
		result.setUrl_md5(url_md5);
		result.setLanguage(language);
		result.setCategory(category);
		result.setSource(source);
		result.setTags(tags);
		result.setCreated_time(created_time);
		result.setUpdated_time(updated_time);
		result.setTags_for_search(tags_for_search);
		result.setSynonyms(synonyms);
		result.setLicense(license);
		result.setHomepage(homepage);
		result.setExtracted_time(extracted_time);
		result.setUpdate_mark(1);//表示是待更新的条目
		if(("FreeCode".toLowerCase()).equals(result.getSource().toLowerCase()) || result.getName() == null || result.getDescription() == null)
			return null;
		return result;
		
	}
	

	/**
	 * 获取合适的id 用最热门项目的id表示
	 * @param 
	 * @return
	 */
	public int getSuitableId(List<GatherProjectsModel> gatherProjectList){
		int smallestId = Integer.MAX_VALUE;
		for(GatherProjectsModel model:gatherProjectList){
			if(model.getId() < smallestId)
				smallestId = model.getId();
		}
		return smallestId;
	}
	
	//获取gatherProjectsList中最合适的项目名
	public String getSuitableName(List<GatherProjectsModel> gatherProjectList){
		String result = null;
//		for(GatherProjectsModel project:gatherProjectList){
//			double tmp = calNameValue(project);
//			if(tmp >= max && project != null){
//				max = tmp;
//				result = project.getName();
//			}
//		}
		//获取每个项目的source 排序为：apache[0]、oschina[1]、openhub[2]、sourceforge[3]
		GatherProjectsModel hottestProject = getHottestProject(gatherProjectList);
		return hottestProject.getName();
	}
	
	//获取最热门项目
	public GatherProjectsModel getHottestProject(List<GatherProjectsModel> gatherProjectList){
//		GatherProjectsModel result = gatherProjectList.get(0);
//		double max_value = calNameValue(result);
//		for(GatherProjectsModel model:gatherProjectList){
//			double value = calNameValue(model);
//			//System.out.println("id:" + model.getId() + "  value:" + value);
//			if(value > max_value){
//				max_value = value;
//				result = model;
//			}
//		}
//		//System.out.println("max id:" + result.getId() + "  value:" + max_value);
//		return result;
		
		int head = Integer.MAX_VALUE;
		GatherProjectsModel result = null;
		for(GatherProjectsModel project:gatherProjectList){
			String source = project.getSource();
			if(source == null)
				source = "";
			int tmp_value = Integer.MAX_VALUE;
			if(source.toLowerCase().equals("apache"))
				tmp_value = 0;
			else if(source.toLowerCase().equals("oschina"))
				tmp_value = 1;
			else if(source.toLowerCase().equals("openhub"))
				tmp_value = 2;
			else if(source.toLowerCase().equals("sourceforge"))
				tmp_value = 3;
			else
				tmp_value = Integer.MAX_VALUE;
			if(head > tmp_value){
				head = tmp_value;
				result = project;
			}else{
				//result = null;
			}
		}
		return result;
	}
	
	//计算项目名的价值
//	public double calNameValue(GatherProjectsModel gatherProject){
//		double result = 0.0;
//		if(gatherProject == null)
//			return result;
//		int visit_num = 0;
//		int download_num = 0;
//		int contributors_num = 0;
//		int followers_num = 0;
//		if(gatherProject.getFollowers_num() == null)
//			followers_num = 0;
//		else
//			followers_num = gatherProject.getFollowers_num();
//		
//		if(gatherProject.getContributors_num() == null)
//			contributors_num = 0;
//		else
//			contributors_num = gatherProject.getContributors_num();
//		
//		if(gatherProject.getDownload_num() == null)
//			download_num = 0;
//		else
//			download_num = gatherProject.getDownload_num();
//		
//		if(gatherProject.getVisit_num() == null)
//			visit_num = 0;
//		else
//			visit_num = gatherProject.getVisit_num();
//		
//		result += followers_num + contributors_num + download_num + visit_num;
//		return result;
//	}

	//合并没有项目源区分的字段 第三个参数num表示返回所有值里面的多少个 -1表示全部返回
	public String mergeTextWithoutSource(String tag1, String tag2, int num){
		if(tag1 == null)
			tag1 = "";
		if(tag2 == null)
			tag2 = "";
		String result = "";
		tag1 = tag1.trim();
		tag2 = tag2.trim();
		String[] tagArray1 = tag1.split(",");
		String[] tagArray2 = tag2.split(",");
		Map<String, Integer> map = new HashMap<String, Integer>();
		for(String tag:tagArray1)
			map.put(tag, 1);
		for(String tag:tagArray2)
			map.put(tag, 1);
		//将map转换为List
		Set<String> keys = map.keySet();
		Iterator<String> it = keys.iterator();
		int count = 0; //用于计数 记录多少个值
		while(it.hasNext()){
			String key = it.next();
			if(!"".equals(key)){
				//如果是空字符串 不添加进去
				result += key + ",";
				count++;
			}
			if(count >= num && num >=0) //为负值表示保留所有结果
				break;
		}
		if(result.length() == 0)
			return null;
		return result.substring(0, result.length() - 1);
	}
	
	//合并有项目源区分的字段
	public String mergeTextWithSource(String text1, String text2, String source){
		//text2是新增的 text1是以前的
		if(text2 == null && text1 != null)
			return text1;
		if(text2 != null && text1 == null){
			return source + "|:|" + text2.trim();
		}
		if(text1 != null && text1.length() != 0){
			return text1 + "|,|" + source + "|:|" + text2.trim();
		}
		return text1;
	}
	
	//合并数字类的记录
	public int mergeInteger(Integer integer1, Integer integer2){
		if(integer1 == null)
			return integer2;
		if(integer2 == null)
			return integer1;
		return integer1 + integer2;
	}
	
	
	//合并日期类记录
	public String mergeTime(String time1, String time2){
		if(time2 == null)
			return time1;
		if(time1 == null)
			return time2;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date1 = sdf.parse(time1);
			Date date2 = sdf.parse(time2);
			if(date1.before(date2)){
				return time1;
			}
			return time2;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return time1;//如果出现错误 原来的时间不变
		}
	}
	
	
	//合并项目来源
	public String mergeSource(String source1, String source2){
		if (source2 == null)
			return source1;
		if (source1 == null)
			return source2;
		String result = "";
		Map<String, Integer> map = new HashMap<String, Integer>();
		String[] array1 = source1.split(",");
		for(String source:array1)
			map.put(source, 1);
		String[] array2 = source2.split(",");
		for(String source:array2)
			map.put(source, 1);
		Set<String> keys = map.keySet();
		Iterator<String> it = keys.iterator();
		while(it.hasNext()){
			String key = it.next();
			result = result + key + ",";
		}
		result = result.substring(0, result.length() - 1);
		return result;
	}
	
	//更新的合并项目来源函数
	public String mergeSource(List<GatherProjectsModel> gatherProjectList, int hottest_id){
		String result = "";
		for(int i = 0; i < gatherProjectList.size(); i++){
			GatherProjectsModel model = gatherProjectList.get(i);
			int id = model.getId();
			String source = model.getSource();
			//判断是不是freecode
			if("freecode".equals(source.toLowerCase())){
				continue;
			}
			if(id == hottest_id){
				result = source + "," + result;
			}else{
				result = result + source + ",";
			}
		}
		result = result.substring(0, result.length() - 1);
		return result;
	}
	
	/**
	 * 查看当前新形成的osp有没有每个id对应的osp_former 使得他们的name tag synonyms属性不相同
	 * 如果有不相同 则删除原来的记录 并插入一条新的记录
	 * 如果全部相同 则不进行后续操作
	 */
	public boolean hasSameOsp(String gather_projects_ids, OpenSourceProject osp){
		boolean result = false;
		String[] idArray = gather_projects_ids.split(",");
		for(String id_str:idArray){
			//查找对应osp记录
			int id = Integer.parseInt(id_str);
			OpenSourceProject osp_former = dbDest.selectOpenSourceProjectsItem(openSourceProjectTableName, id);
			if(osp_former != null){
				if(sameName(osp_former, osp) && sameTags(osp_former, osp) && sameSynonyms(osp_former, osp) && result == false){
					result = true;
					continue;//认为项目相同 不替换
				}else{
					//删除osp_former
					dbDest.deleteOpenSourceProjectsItem(openSourceProjectTableName,id); 
					dbSource.deleteTaggingsByTaggableId(taggingTableName, id);	//由于没有级联删除 因此需要手动删除
					dbDest.deleteMatchResult(id, getTargetTable(id));
				}
			}
		}
		return result;
	}
	
	
	
	/**
	 * 比较两个osp对象的姓名是否相同
	 */
	public boolean sameName(OpenSourceProject osp_former, OpenSourceProject osp){
		if((osp_former.getName().toLowerCase()).equals(osp.getName().toLowerCase()))
			return true;
		else
			return false;
	}
	
	/**
	 * 比较两个osp对象的标签是否相同
	 */
	public boolean sameTags(OpenSourceProject osp_former, OpenSourceProject osp){
		String tags_former = osp_former.getTags();
		String tags = osp.getTags();
		if(tags_former == null && tags == null)
			return true;
		else if((tags_former != null && tags == null) || (tags_former == null && tags != null))
			return false;
		Map<String, Integer> tagMap_former = new HashMap<String, Integer>();
		Map<String, Integer> tagMap = new HashMap<String, Integer>();
		String[] tagArray_former = tags_former.split(",");
		String[] tagArray = tags.split(",");
		for(String tag:tagArray_former)
			tagMap_former.put(tag, 1);
		for(String tag:tagArray)
			tagMap.put(tag, 1);
		
		for(String tag:tagArray_former)
			if(!tagMap.containsKey(tag))
				return false;
		
		for(String tag:tagArray)
			if(!tagMap_former.containsKey(tag))
				return false;
		
		//如果相互之间每个tag都存在 则证明两个tags属性完全相同
		return true;
	}
	
	/**
	 * 比较两个osp对象的同义词是否相同
	 */
	public boolean sameSynonyms(OpenSourceProject osp_former, OpenSourceProject osp){
		String synonyms_former = osp_former.getSynonyms();
		String synonyms = osp.getSynonyms();
		if(synonyms_former == null && synonyms == null)
			return true;
		else if((synonyms_former != null && synonyms == null) || (synonyms_former == null && synonyms != null))
			return false;
		Map<String, Integer> synonymMap_former = new HashMap<String, Integer>();
		Map<String, Integer> synonymMap = new HashMap<String, Integer>();
		String[] synonymArray_former = synonyms_former.split(",");
		String[] synonymArray = synonyms.split(",");
		for(String synonym:synonymArray_former)
			synonymMap_former.put(synonym, 1);
		for(String synonym:synonymArray)
			synonymMap.put(synonym, 1);
		
		for(String synonym:synonymArray_former)
			if(!synonymMap.containsKey(synonym))
				return false;
		
		for(String synonym:synonymArray)
			if(!synonymMap_former.containsKey(synonym))
				return false;
		
		//如果相互之间每个synonym都存在 则证明两个synonyms属性完全相同
		return true;
	}
	
	
	/**
	 * 将gather_projects_ids转换为最热门项目id在前的
	 */
	public List<GatherProjectsModel> putHottestInHead(List<GatherProjectsModel> gatherProjectList, int hottest_id){
		for(int i = 0; i < gatherProjectList.size(); i++){
			GatherProjectsModel model = gatherProjectList.get(i);
			int id = model.getId();
			if(id == hottest_id){
				//删除当前下标的 再插入到最前面
				gatherProjectList.remove(i);
				gatherProjectList.add(0, model);
				break;
			}
		}
		return gatherProjectList;
	}
	
	
	/**
	 * 与mergeProjects里面的函数区别为 不考虑数据库中已经提取出来的情况
	 * @param model
	 * @return
	 */
	public List<String> getSynonyms(GatherProjectsModel model){
		List<String> result = new ArrayList<String>();
		String description = model.getDescription();
		if(description == null)
			return result;

		model.setName(model.getName().trim());
		model.setDescription(model.getDescription().trim());
		//最终返回的同义词列表
		List<String> synonymsList = new ArrayList<String>();
		List<String> linkNameList = new ArrayList<String>();//用于存储所有提取出来的关联项目名
		List<String> keywords = FileReader.read("./files/language.txt");
		
		//加入项目名含括号，加入括号中的内容
		String prjName = model.getName();
		String prjNameNoBracket = StringHandler_ProjectName.removeBracket(prjName);
		if(prjName.indexOf("(") != -1 || prjName.indexOf("（")!=-1){
			synonymsList.add(prjNameNoBracket);
		}
		
		//提取项目名括号中的信息，不是项目类型
		String bracket = StringHandler_ProjectName.getBracket(model.getName());
		if(!"".equals(bracket)){
			bracket = RegexHandler.extractEngDecChiAndDot(bracket);//去除括号中信息的特殊符号
			if(!RegexHandler.onlySpecialChar(bracket)){
				if(!keywords.contains(bracket)){//括号中的信息不是语言类关键字词
					linkNameList.add(bracket);
					}
			}
		}
		
		//对项目名进行处理,提取项目名中去除公司名或基金组织的信息
		String projectNameWithoutComName = StringHandler_ProjectName.getProjectWithoutComName(model.getName());
		projectNameWithoutComName = StringHandler_ProjectName.removeBracket(projectNameWithoutComName);
		if(!"".equals(projectNameWithoutComName)){
			//表示匹配到了相应公司或基金委的项目
			synonymsList.add(projectNameWithoutComName);
		}
		
		
		if(description != null){
			
			List<String> beWords = FileReader.read("./files/beVerb.txt");//读取be动词表
			description = StringHandler.getFirstSentence(description);
//			if (description.indexOf(":") != -1){//提取描述中冒号之前的信息
//				int indexColon = description.indexOf(":");
//				String beforeColon = description.substring(0, indexColon);
//				linkNameList.add(beforeColon.trim());
//			}
				
			for(String beWord:beWords){
				
				String linkName = StringHandler.findLinkName(description, beWord);//按系动词表优先级进行匹配
				if(linkName == null){//表示没有匹配到当前的系动词 进行下一个匹配
					continue;
				}
				//特征短语去噪
				//提取主副描述信息(括号中)
				List<String> linkNameResult = EDDHandler.getMainAndViceDescriptionByBracket(linkName);
				
				//提取主副描述信息(known as+连词)
				linkNameResult = EDDHandler.getMainAndViceDescriptionByKnowAs(linkNameResult);
				//去除定语描述信息(逗号定语)
				linkNameResult = EDDHandler.removeComma(linkNameResult);
				//去除定语描述信息(定冠词The)
				linkNameResult = EDDHandler.removeDemonstrativeWords(linkNameResult);
				//去除从句描述信息
				linkNameResult = EDDHandler.removeArrtibutiveClause(linkNameResult);
				//去除指示代词
				linkNameResult = EDDHandler.removePronoun(linkNameResult);
				//去除项目常用词
				linkNameResult = EDDHandler.removeProject(linkNameResult);
				//去除指示代词和项目常用词两者的笛卡尔积
				linkNameResult = EDDHandler.removePPCombine(linkNameResult);
				//提取项目中
				linkNameResult = RegexHandler.extractEngDecChiAndDot(linkNameResult);
				//删除只有特殊字符的同义词
				linkNameResult = RegexHandler.removeOnlySpecial(linkNameResult);
				for(String name:linkNameResult){
//System.out.println("be 动词之前的有： "+name);
					//be动词前不包括项目类型，且和项目名不相同
				    if(!keywords.contains(name) && !name.equals(model.getName().toLowerCase())){
				    	if(model.getName().toLowerCase().indexOf(name) != -1 && !name.equals(prjNameNoBracket))
				    		synonymsList.add(name);
				    	else
				    		linkNameList.add(name);
				    }
				    	
				}
			}
		}
		//result包含去掉公司信息、括号内的、be动词之前的
		result = new ArrayList(new HashSet(linkNameList));
//System.out.println("result包含去掉公司信息、括号内的、be动词之前的;");
		//for (int i = 0;i<result.size();i++)
			//System.out.println("result: "+result.get(i));
		
		String fullName = StringHandler.getFullName(prjNameNoBracket.toLowerCase(), result);
		if(!fullName.equals("")){
			synonymsList.add(fullName.toLowerCase());//表示项目名确实是缩写 并提取到了项目全名
//System.out.println("项目的全称为： "+fullName);
		}
		
		
		String shortName = StringHandler.getShortName(prjNameNoBracket.toLowerCase(), result);
		if(!shortName.equals("")){
			synonymsList.add(shortName.toLowerCase());//表示项目名确实是全称 并提取到了项目缩写
//System.out.println("项目的简称为： "+shortName);
		}
		
		//同义词要和项目名称有一定的相似度
		for(int i = 0; i < result.size(); i++){
			String extract = "";
			String synonym = result.get(i);
			String[] words = synonym.split(" ");//按照空格进行分词
			for(String word:words){
				if(model.getName().indexOf(word) >= 0 ||model.getName().toLowerCase().indexOf(word) >= 0){//查看每个词在项目名中是否出现
					extract += word + " ";
				}
			}
			if(extract.length() !=0)
				extract = extract.substring(0, extract.length() - 1);
//System.out.println("extract : "+extract);
			if(!"".equals(extract) && !keywords.contains(extract)){
				//提取的别名不和项目名重复
				if(!extract.toLowerCase().equals(model.getName().toLowerCase().trim()) && !extract.toLowerCase().trim().equals(prjNameNoBracket.toLowerCase().trim())){
					double similarDegree = SimilarDegree(extract,prjNameNoBracket);
//System.out.println("字符串间的相似度为: "+similarDegree);
					if(similarDegree >= 0.4 && similarDegree <1.0){
//System.out.println("别名在项目名中有出现： "+extract);
					synonymsList.add(extract);
					}
				}
			}

		}
		//添加项目名本身，用于利用别名寻找匹配项目时
		synonymsList.add(model.getName().toLowerCase());
		synonymsList = new ArrayList(new HashSet(synonymsList));
		//输出日志文件
		for(String s : synonymsList){
			logger.info("gather_project:" + model.getId() + " gets synonyms:" + s + " by description");
		}
		//去掉重复元素
		return  synonymsList;
	}
	public static double SimilarDegree(String strA, String strB){  
		  
        String newStrA = removeSign(strA);  
        String newStrB = removeSign(strB);  
        int temp = Math.max(newStrA.length(), newStrB.length());  
        int temp2 = longestCommonSubstring(newStrA, newStrB).length();  
        return temp2 * 1.0 / temp;  
  
    }  
  
    private static String removeSign(String str) {  
        StringBuffer sb = new StringBuffer();  
        for (char item : str.toCharArray())  
            if (charReg(item)){  
                sb.append(item);  
            }  
        return sb.toString();  
  
    }  
  
  
    private static boolean charReg(char charValue) {  
  
        return (charValue >= 0x4E00 && charValue <= 0X9FA5)  
                || (charValue >= 'a' && charValue <= 'z')  
                || (charValue >= 'A' && charValue <= 'Z')  
                || (charValue >= '0' && charValue <= '9');  
  
    }  
  
    private static String longestCommonSubstring(String strA, String strB) {  
        char[] chars_strA = strA.toCharArray();  
        char[] chars_strB = strB.toCharArray();  
        int m = chars_strA.length;  
        int n = chars_strB.length;  
        int[][] matrix = new int[m + 1][n + 1];  
        for (int i = 1; i <= m; i++) {  
  
            for (int j = 1; j <= n; j++) {  
                if (chars_strA[i - 1] == chars_strB[j - 1])  
                    matrix[i][j] = matrix[i - 1][j - 1] + 1;  
                else  
                    matrix[i][j] = Math.max(matrix[i][j - 1], matrix[i - 1][j]);  
            }  
  
        }  
  
        char[] result = new char[matrix[m][n]];  
        int currentIndex = result.length - 1;  
        while (matrix[m][n] != 0) {  
            if (matrix[m][n] == matrix[m][n - 1])  
                n--;  
            else if (matrix[m][n] == matrix[m - 1][n])   
                m--;  
            else {  
                result[currentIndex] = chars_strA[m - 1];  
                currentIndex--;  
                n--;  
                m--;  
            }  
        }  
        return new String(result);  
  
    }  

}
